using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Xml.Serialization;

using dataHelper = CatalogManagerDAL.DataHelper;
using Microsoft.CommerceServer.Catalog;

namespace CatalogManagerUnitTests
{
    /// <summary>
    /// Summary description for CreateEntitiesTest
    /// </summary>
    [TestClass]
    public class CreateEntitiesTest
    {
        public CreateEntitiesTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void CreateProductInCatalogWithBaseExtendedAndInventoryProperties()
        {
            // arrage
            DataTable ProductDataBaseAndExtended = new DataTable();
            //"SerializedObjects/CreateEntities_ProductData1.xml");
            string FilePath = @"../CatalogManagerUnitTests/SerializedObjects/CreateEntities_ProductData1.xml";
            ProductDataBaseAndExtended.ReadXml(FilePath);
            string CatalogName = "44000000";
            string ProductDefinition = "Film";
            string ProductId = "ZAM654321";

            CatalogManagerBL.CreateEntities BL_CreateEntities = new CatalogManagerBL.CreateEntities();

            // act
            ProductFamily productFamily = null;
            CatalogItem product = null;
            try
            {
                BL_CreateEntities.CreateProductOrProductFamily(ProductId, ProductDefinition, CatalogName, ref productFamily, ref product);

                BL_CreateEntities.CreateProduct(
                    product, CatalogName, "", ProductDefinition, ProductId, ProductDataBaseAndExtended, null
                );
            }
            catch (Microsoft.CommerceServer.EntityAlreadyExistsException ex )
            {
                Console.WriteLine(ex.Message);
            }
            
            // assert
            CatalogContext CommerceCtx = dataHelper.InitContext();
            product = CommerceCtx.GetProduct(CatalogName, ProductId);
            Assert.IsNotNull(product);
            BaseCatalog catalog = CommerceCtx.GetCatalog(CatalogName) as BaseCatalog;

            if (null != catalog) catalog.DeleteProduct(ProductId);
            product = null;
            try
            {
                product = CommerceCtx.GetProduct(CatalogName, ProductId);
            }
            catch (Microsoft.CommerceServer.EntityDoesNotExistException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Assert.IsNull(product);
        }

        [TestMethod]
        public void CreateCategory()
        {
            // arrage
            string CatalogName = "ZamTv";
            string CategoryDefinition = "DefaultCategory";
            string CategoryName = "Test2_"+DateTime.Now.Millisecond;
            CatalogManagerDAL.CreateEntities DAL_CreateEntities = new CatalogManagerDAL.CreateEntities();

            // act
            try
            {
                DAL_CreateEntities.CreateCategory_OnlyWithName(
                    CategoryDefinition, CatalogName, CategoryName
                );
            }
            catch (Microsoft.CommerceServer.EntityAlreadyExistsException ex)
            {
                Console.WriteLine(ex.Message);
            }

            // assert
            CatalogContext CommerceCtx = dataHelper.InitContext();
            BaseCatalog catalog = CommerceCtx.GetCatalog(CatalogName, "tr-TR") as BaseCatalog;
            Category category = catalog.GetCategory(CategoryName);

            Assert.IsNotNull(category);

            if (null != catalog) catalog.DeleteCategory(CategoryName);
            category = null;
            try
            {
                category = CommerceCtx.GetCategory(CatalogName, CategoryName, "tr-TR");
            }
            catch (Microsoft.CommerceServer.EntityDoesNotExistException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Assert.IsNull(category);
        }
    }
}
